//获取登录token
function getTokenStorage() {
	let token = uni.getStorageSync('token');
	return token;
}

//设置登录token
function setTokenStorage(token) {
	uni.setStorageSync('token', token);
}

//设置用户Id
function setUserId(userid) {
	uni.setStorageSync("userId", userid);
}

//获取用户Id
function getUserId() {
	let userid = uni.getStorageSync("userId");
	return userid;
}

//设置站点Id
function setSiteId(siteId) {
	uni.setStorageSync("siteId", siteId)
}

//获取站点Id
function getSiteId() {
	let siteId = uni.getStorageSync("siteId");
	return siteId;
}

//格式化年月日
function formatDate(now) {
	const date = new Date(now);
	var y = date.getFullYear();
	var m = date.getMonth() + 1;
	var d = date.getDay();
	return y + '-' + m + '-'+d;
}

function formatDateYear(now) {
	const date = new Date(now);
	var y = date.getFullYear();
	var m = date.getMonth() + 1;
	return y + '-' + m;
}

//遍历两个日期之间所有月份
function getAllMonth(start, end) {
	let result = [];
	let starts = start.split('-');
	let ends = end.split('-');
	let staYear = parseInt(starts[0]);
	let staMon = parseInt(starts[1]);
	let endYear = parseInt(ends[0]);
	let endMon = parseInt(ends[1]);
	while (staYear <= endYear) {
		if (staYear === endYear) {
			while (staMon < endMon) {
				
				var str = staYear + '-' + (staMon >= 10 ? staMon : '0' + staMon);
				result.push(str);
				staMon++;
			}
			staYear++;
		} else {
			let str = staYear + '-' + (staMon >= 10 ? staMon : '0' + staMon);
			result.push(str);
			staMon++;
			if (staMon > 12) {
				staMon = 1;
				staYear++;
			}
		}
	}
	console.log(result);
	return result;
}

/*
* 获取上一年的月份遍历
*/
function getLastMonth(start,end)
{
	let result = [];
	let starts = start.split('-');
	let ends = end.split('-');
	let staYear = parseInt(starts[0]);
	let staMon = parseInt(starts[1]);
	let endYear = parseInt(ends[0]);
	let endMon = parseInt(ends[1]);
	
	return getAllMonth(staYear-1+'-'+staMon,endYear-1+"-"+endMon);
}

function getLastMonth_1(start,end){
	let date = new Date(start);
	date.setMonth(date.getMonth() - 1);
	console.log(start)
	var starty = date.getFullYear();
	var startm = date.getMonth() + 1;
	let result = getAllMonth(starty+"-"+startm,end)
	// console.log(result);
	return result;
}

function numToTime(time){
	time.forEach((item,index)=>{
		console.log(item);
		console.log(index);
	});
}

export {
	getTokenStorage,
	setTokenStorage,
	setUserId,
	getUserId,
	setSiteId,
	getSiteId,
	formatDate,
	getAllMonth,
	getLastMonth,
	getLastMonth_1,
	numToTime,
	formatDateYear
};